﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using TPLinkNetworking;

namespace HomeSuiteHome
{
    public partial class Form1 : Form
    {
        static int _upCount, _sendCount;
        List<Module> _smartLinks = new List<Module>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            bw_IpScanner.RunWorkerAsync();
        }

        private void bw_IpScanner_DoWork(object sender, DoWorkEventArgs e)
        {
            
            foreach (var localIpAddress in GetLocalIpAddresses)
            {
                var baseIpAddress = localIpAddress.ToString()
                    .Substring(0, localIpAddress.ToString().LastIndexOf('.'));
                for (var i = 1; i < 255; i++)
                {
                    var ipAddress = baseIpAddress + "." + i;
                    var pingHost = new Ping();
                    pingHost.PingCompleted += PingHostOnPingCompleted;
                    pingHost.SendAsync(ipAddress, 2000, ipAddress);
                    _sendCount++;
                }
            }
        }

        private void PingHostOnPingCompleted(object sender, PingCompletedEventArgs e)
        {
            var ip = (string)e.UserState;
            if (e.Reply == null || e.Reply.Status != IPStatus.Success) return;
            var tpConnection = new TPLinkConnection();
            var smartLinkConnect = new TPLinkConnection.SmartLink {LinkAddress = ip};
            var moduleInformation = tpConnection.GetModuleInformation(smartLinkConnect);
            if (moduleInformation == null) return;
            smartLinkConnect.ModuleInformation = moduleInformation;
            var newModule = new Module(smartLinkConnect) {Dock = DockStyle.Top};
            panel_Modules.Invoke((MethodInvoker) delegate { panel_Modules.Controls.Add(newModule); });
            _smartLinks.Add(newModule);
        }

        private void tmr_ModuleCheck_Tick(object sender, EventArgs e)
        {
            //if (!ModuleCheck.IsBusy)
            //    ModuleCheck.RunWorkerAsync();
            foreach (var smartLink in _smartLinks)
            {
                smartLink.UpdateModuleInformation();

            }
        }

        private void ModuleCheck_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach (var smartLink in _smartLinks)
            {
                smartLink.UpdateModuleInformation();

            }
        }

        private static IEnumerable<IPAddress> GetLocalIpAddresses
        {
            get
            {
                var host = Dns.GetHostEntry(Dns.GetHostName());
                return host.AddressList.Where(ip => ip.AddressFamily != AddressFamily.InterNetworkV6).ToArray();
            }
        }
    }
}
